﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using DAL;

namespace PeisongWeb.admin
{
    public partial class companySupplyRangeEdit : System.Web.UI.Page
    {
        protected int companyId
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["companyId"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["companyId"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["company"] != null)
                {
                    if (Library.Number.IsIntPositive(Request.QueryString["company"]))
                    {
                        companyId = Convert.ToInt32(Request.QueryString["company"]);
                        span_Company.InnerHtml = logic.company.getById(companyId).Company;
                    }
                    else
                    {
                        Library.Script.ClientUrl("companyList.aspx");
                        return;
                    }
                }
                else
                {
                    Library.Script.ClientUrl("companyList.aspx");
                    return;
                }

                drpBind();
                sellerBusinessNatureBind();
                rptBind();
            }
        }

        private void sellerBusinessNatureBind()
        {
            DataTable dt = logic.company.getCompanyBusinessNature(companyId);

            DataView dv = new DataView(dt);
            dv.RowFilter = "isChecked=1";

            ddlProductBus.DataSource = dv;
            ddlProductBus.DataBind();
            ddlProductBus.Items.Insert(0, new ListItem("-所有等级-", ""));
        }

        private void drpBind()
        {
            drp_CategoryRoot.DataSource = logic.category.listWithCompanyRange(companyId);
            drp_CategoryRoot.DataBind();
            drp_CategoryRoot.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
            categorySubBind();

        }

        void categorySubBind()
        {
            if (drp_CategoryRoot.SelectedValue != "")
            {
                DataTable dt = logic.category.getByParent(Convert.ToInt32(drp_CategoryRoot.SelectedValue));
                drp_CategorySub.DataSource = dt;
                drp_CategorySub.DataBind();
                drp_CategorySub.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
            }
        }

        void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.AppendFormat(" companyId={0} and not exists (select 1 from dbo.vw_SellerRange_list where productId=vw_sellerRangeProduct_withBusinessNature.productId and businessNature=vw_sellerRangeProduct_withBusinessNature.businessNatureRange and companyId={0})", companyId);

            if (!string.IsNullOrEmpty(drp_CategoryRoot.SelectedValue))
                where.AppendFormat(" and categoryParentId={0}", drp_CategoryRoot.SelectedValue);
            if (drp_CategorySub.SelectedValue.ToString().Trim() != "")
                where.AppendFormat(" and categoryId={0}", drp_CategorySub.SelectedValue);
            if (!string.IsNullOrEmpty(txtProductName.Text))
                where.AppendFormat(" and [name] like '%{0}%'", txtProductName.Text);
            if (!string.IsNullOrEmpty(txtProductNo.Text.Trim()))
                where.AppendFormat(" and productNo='{0}'", txtProductNo.Text);
            if (!string.IsNullOrEmpty(ddlProductBus.SelectedValue))
                where.AppendFormat(" and businessNatureRange='{0}'", ddlProductBus.SelectedValue);

            DataSet ds = logic.sellerRange.listWithBusinessNature(where.ToString(), "productId,businessNatureRange", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }

        protected void rpt_list_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "SelectProduct")
            {
                string[] array=e.CommandArgument.ToString().Split(new char[] { ',' },StringSplitOptions.RemoveEmptyEntries);
                int productId = Convert.ToInt32(array[0]);
                string businessNature = array[1];

                SellerRangeEntity sr = new SellerRangeEntity();
                sr.CompanyId = companyId;
                sr.ProductId = productId;
                sr.Rate = Convert.ToDecimal(1);
                sr.businessNature = businessNature;
                logic.sellerRange.insert(sr);

                rptBind();
                Library.Script.ClientMsg("添加成功！");
            }
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }
        protected void lbtn_Search_Click(object sender, EventArgs e)
        {
            rptBind();
        }

        protected void ddl_CategoryRoot_SelectedIndexChanged(object sender, EventArgs e)
        {
            categorySubBind();
        }
        // 添加选择项
        protected void btnDelSelected_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(hdSelectedProductIds.Value))
            {
                logic.sellerRange.insertBySelected(hdSelectedProductIds.Value, companyId);
                rptBind();

                Library.Script.ClientMsg("添加成功.");
            }
        }
    }
}
